const app = getApp();
const http = require('../../../utils/http.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        TabCur: 1,
        SortMenu: '',
        pageNum1: 1,
        pageNum2: 1,
        pageNum3: 1,
        pageSize: 15,
        tradeList: [],
        couponOrder: [],
        Page2List: [],
        customer:null,
        itemList: ['按日期', '按月份', '按年限'],
        timeTxt:'按日期',
        timeNum:10
    },
    timeBtn: function(e) {
        var that=this
        wx.showActionSheet({
            itemList: this.data.itemList,
            success (res) {
                console.log(res.tapIndex)//4年  7月  10日
                if(res.tapIndex==0){
                    that.setData({
                        timeNum:10
                    })
                }
                if(res.tapIndex==1){
                    that.setData({
                        timeNum:7
                    })
                }
                if(res.tapIndex==2){
                    that.setData({
                        timeNum:4
                    })
                }
                that.setData({
                    timeTxt:that.data.itemList[res.tapIndex]
                })
                that.statisticsCustomerPage2()
            },
            fail (res) {
                console.log(res.errMsg)
            }
        })
    },

    tabSelect(e) {
        this.setData({
            TabCur: e.currentTarget.dataset.id,
            customer: app.globalData.customer
        })
        if (this.data.TabCur == 1) {
            this.selectCouponOrder()
        } 
        if (this.data.TabCur == 2) {
            this.selectTradeList()
        }
        if (this.data.TabCur == 3){
            this.statisticsCustomerPage2()
        }
    },
    dcBtn:function(){
        wx.showModal({
            title: '提示',
            content: '你是否确定退出登录？',
            success (res) {
              //console.log('asd')
              if (res.confirm) {
                //console.log('用户点击确定')
                wx.removeStorage({
                    key: 'sysUserId',
                    success (res) {
                      //console.log(res)
                      wx.navigateTo({
                        url: '/pages/merchant/login/index',
                      })
                    }
                  })
              } else if (res.cancel) {
                //console.log('用户点击取消')
              }
            }
          })
    },

    checkLogin() {
        let sysUserId = wx.getStorageSync("sysUserId")
        if (!sysUserId) {
            wx.navigateTo({
                url: '/pages/merchant/login/index'
            })
        } else {
            if (this.data.TabCur == 1) {
                this.selectCouponOrder()
            } 
            if (this.data.TabCur == 2){
                this.selectTradeList()
            }
            if (this.data.TabCur == 3){
                this.statisticsCustomerPage2()
            }
        }
    },

    selectTradeList() {
        http.get('/api/merchant/statisticsCustomerPage', {pageNum: 1, pageSize: this.data.pageSize}).then(res => {
            if (res.code == 200) {
                this.setData({
                    tradeList: res.data.list,
                    hasNextPage: res.data.hasNextPage,
                    pageNum2:2
                })
            }
        })
    },
    selectCouponOrder() {
        http.get('/api/merchant/selectCouponOrder', {pageNum: 1, pageSize: this.data.pageSize}).then(res => {
            if (res.code == 200) {
                this.setData({
                    SortMenu: [{id: 1, name: "优惠券核销记录(" + res.data.total + ")"}, {id: 2, name: "交易记录"}, {id: 3, name: "核销统计"}],
                    couponOrder: res.data.list,
                    hasNextPage: res.data.hasNextPage,
                    pageNum1:2
                })
            }
        })
    },
    statisticsCustomerPage2() {
        http.get('/api/merchant/statisticsCustomerPage2', {pageNum: 1, pageSize: this.data.pageSize,groupNo: this.data.timeNum}).then(res => {
            if (res.code == 200) {
                this.setData({
                    Page2List: res.data.list,
                    hasNextPage: res.data.hasNextPage,
                    pageNum3:2
                })
            }
        })
    },
    tradeclick(e) {
        let item = e.currentTarget.dataset;
        if(item.num=='num'){
            wx.navigateTo({
                url: '/pages/merchant/tradeList2/index?time=' + item.time
            });
        }else{
            wx.navigateTo({
                url: '/pages/merchant/tradeList/index?time=' + item.time
            });
        }
        
    },

    scan: function (e) {
        const that = this
        wx.scanCode({
            success(res) {
                console.log(res.data)
                http.get("/api/merchant/useMerCoupon", {couponCode: res.result}).then(res=> {
                    wx.showModal({
                        title: '提示',
                        content: res.message,
                        showCancel: false
                    })
                    that.selectCouponOrder();
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            customer: app.globalData.customer
        })
        this.checkLogin()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.checkLogin()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        console.log("下拉刷新")
        var TabCur = this.data.TabCur
        if (TabCur == 1) {
            this.selectCouponOrder()
            this.setData({
                pageNum1: 1
            })
        } 
        if (TabCur == 2) {
            this.selectTradeList()
            this.setData({
                pageNum2: 1
            })
        }
        if (TabCur == 3) {
            this.statisticsCustomerPage2()
            this.setData({
                pageNum3: 1
            })
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log("上拉分页")
        var TabCur = this.data.TabCur
        var url, listTemp, pageNum, list
        const that = this
        wx.showLoading({
            title: '加载中。。。',
            mask: true
        })
        if (TabCur == 1) {
            url = '/api/merchant/selectCouponOrder';
            pageNum = that.data.pageNum1;
            list = that.data.couponOrder;
        } 
        if (TabCur == 2) {
            url = '/api/merchant/statisticsCustomerPage';
            pageNum = that.data.pageNum2;
            list = that.data.tradeList;
        }
        if (TabCur == 3) {
            url = '/api/merchant/statisticsCustomerPage2';
            pageNum = that.data.pageNum3;
            list = that.data.Page2List;
        }
        if (this.data.hasNextPage) {
            http.get(url, {pageNum: pageNum, pageSize: this.data.pageSize}).then(res => {
                wx.hideLoading()
                if (res.code == 200) {
                    if (TabCur == 1) {
                        this.setData({
                            pageNum1: res.data.pageNum + 1,
                            hasNextPage: res.data.hasNextPage,
                            couponOrder: list.concat(res.data.list)
                        })
                    } else {
                        this.setData({
                            pageNum2: res.data.pageNum + 1,
                            hasNextPage: res.data.hasNextPage,
                            tradeList: list.concat(res.data.list)
                        })
                    }
                }
            })
        } else {
            wx.showToast({
                title: '没有更多数据',
                icon: "none"
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },


})
